<template>
<el-drawer class="drawer" :title="drawer.title" :visible.sync="drawer.visible" direction="rtl" custom-class="drawer-right-side" :before-close="closeDrawer" :append-to-body="true">

    <el-form class="ruleForm" ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" label-width="100px">
        <el-form-item label="商品分类" prop="category_id">
            <picker-view v-model="ruleForm.category_id" type="category" placeholder="商品分类"></picker-view>
        </el-form-item>

        <el-form-item label="所属公司" prop="company_id">
            <picker-view v-model="ruleForm.company_id" type="company" placeholder="所属公司"></picker-view>
        </el-form-item>

        <el-form-item label="供应商" prop="supplier_id">
            <picker-view v-model="ruleForm.supplier_id" type="supplier" placeholder="供应商"></picker-view>
        </el-form-item>

        <el-form-item label="商品名称" prop="title">
            <el-input v-model="ruleForm.title" placeholder="商品名称"></el-input>
        </el-form-item>

        <el-form-item label="商品价格" prop="price">
            <el-input v-model="ruleForm.price" placeholder="商品价格"></el-input>
        </el-form-item>

        <el-form-item label="单位" prop="unit">
            <el-input v-model="ruleForm.unit" placeholder="商品价格"></el-input>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
            <el-input v-model="ruleForm.sort" type="number" placeholder="排序"></el-input>
        </el-form-item>

        <el-form-item label="上下线">
            <el-radio-group v-model="ruleForm.status">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
            </el-radio-group>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" size="medium" @click="submit">确定并提交</el-button>
        </el-form-item>
    </el-form>
</el-drawer>
</template>

<script>
import {
    getFormToken
} from "@/api/system/system";

import pickerView from "@/components/picker/select.vue";

import {
    createProduceApi,
    editProduceApi
} from "@/api/product/produce";

const ruleForm = {
    id: "",
    company_id: "",
    category_id: "",
    title: "",
    price: "",
    unit: "",
    sort: 0,
    status: 1,
    form_token: "",
};

export default {
    components: {
        pickerView
    },
    data() {
        return {
            drawer: {
                title: "添加商品信息",
                visible: false,
                type: "create",
            },
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                title: [{
                    required: true,
                    message: "请填写商品名称",
                    trigger: "blur",
                }, ],
                category_id: [{
                    required: true,
                    message: "请选择商品分类",
                    trigger: "change",
                }, ],
                unit: [{
                    required: true,
                    message: "请填写商品单位",
                    trigger: "blur",
                }, ],
            },
        };
    },
    computed: {
        companyRules: function() {
            return [{
                required: true,
                message: "请填写公司名称",
                trigger: "blur",
            }];
        }
    },
    methods: {
        showCreate() {
            this.ruleForm = Object.assign({}, ruleForm);
            this.drawer.visible = true;
            this.drawer.type = "create";
            getFormToken({
                api: "/api/product.produce/create",
            }).then((res) => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        closeDrawer() {
            this.drawer.visible = false;
        },
        showEdit(row) {
            this.ruleForm = Object.assign({}, row);
            this.drawer.title = "编辑商品信息";
            this.drawer.visible = true;
            this.drawer.type = "edit";
            getFormToken({
                api: "/api/product.produce/edit",
            }).then((res) => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        submit() {
            this.$refs["ruleForm"].validate((valid) => {
                if (!valid) return;

                if (this.drawer.type == "create") {
                    createProduceApi(this.ruleForm).then((res) => {
                        if (res.code == "200") {
                            this.$emit("getTableData");
                            this.closeDrawer();
                            this.$message.success(res.msg);
                        } else {
                            this.$message.error(res.msg);
                        }
                    });
                } else {
                    editProduceApi(this.ruleForm).then((res) => {
                        if (res.code == "200") {
                            this.$emit("getTableData");
                            this.closeDrawer();
                            this.$message.success(res.msg);
                        } else {
                            this.$message.error(res.msg);
                        }
                    });
                }
            });
        },
    },
};
</script>

<style>
.contacts {
    margin: 18px 0;
}

.w100 {
    width: 100%;
}
</style>
